package inc.jmc.cyberboard.ui.print;
/*
 * IncomingMessagesThread.java
 *
 * Created on 5 de octubre de 2006, 10:48 AM
 *
 
 */
import inc.jmc.cyberboard.ui.CyberManager;
import inc.jmc.cyberboard.ui.print.vo.ImpresionJobBean;

import java.io.ObjectInputStream;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import org.apache.log4j.Logger;
/**
 *
 * @author Juan Miguel Olguin Salguero
 *
 **TEXTO DE INFORMACION PARA EL BLOQUEO DE LOS mensages de entrada
 *Set your firewall to block UDP and TCP ports 135, 137-139, and port 445 from all incoming requests or data.
 *A reboot may be required for changes to take effect depending on your firewall software.
 *Please Note: That while blocking UDP and TCP ports 135, 137-139 and 445 will prevent 
 * the webpopup spam from reaching you, it may also block such programs such as Microsoft Outlook 
 * which use port 135 to communicate. If you discover certain software fails to work after blocking the ports 
 * mentioned above, most firewall software allows the user to designate certain software for pass-through access. 
 * This must be set manually by the user. Please see your firewall's help section to learn how to do this.
 *
 *
 *
 */
public class IncomingPrintJobsThread extends Thread {
    
	public static  int LISTENING_PORT=12269;
	
	
    static Logger logger = Logger.getLogger(IncomingPrintJobsThread.class.getName());
	
    private CyberManager cyberM;
    int port;
    InetAddress localAddress;
    public IncomingPrintJobsThread(CyberManager cyberM,int port )throws Exception {
        this.cyberM=cyberM;
        this.port=port;
        this.localAddress=InetAddress.getLocalHost();
        logger.debug("puerto:"+this.port);
        logger.debug("inetaddress:"+this.localAddress);
    }
    boolean escucho=true;
    public void stopListening()
    {
        escucho=false;
    }
    
    public void run() {
             
        try{
            logger.debug("Thread iniciado y corriendo");
            ServerSocket socketRecept=null;
            try{
            socketRecept=new ServerSocket(port) ;
            }
            catch(java.net.BindException ex){
                port+=10;
                socketRecept=new ServerSocket(port) ;
                
            }
            
            logger.info("Puerto activo para recibir alarmas de impresion"+(port));
            logger.debug("esperando notificaciones de impresion...");
            while(escucho)
              {
                //int car=12;
                Socket inSocket=socketRecept.accept();
                logger.debug("llego una notificacion de impresion");
                java.io.InputStream is=inSocket.getInputStream();
                ObjectInputStream objInput = new ObjectInputStream(is);
                // Deserialize the object
                ImpresionJobBean jobBean = (ImpresionJobBean) objInput.readObject();
                cyberM.getPrintjobs().addTareaDeImpresion(jobBean);
                logger.debug("--ya llego el objeto de notificacion de impresion--");
                logger.debug(jobBean.toString());
                objInput.close(); //cerramos el flujo del objeto serializado
                inSocket.close();//cerramos el socket
              }
            
            logger.debug("El threar "+this.getName()+" ya dejo de atender peticiones");
        }
        catch(Exception ex)
              {
                ex.printStackTrace();
              }
         }
    
    
}
